package com.sxz.sbs.module.staff;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseActivity;
import com.sxz.sbs.constant.TypeConstant;
import com.sxz.sbs.event.OperateSuccessEvent;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.client.adapter.AllHousesAdapter;
import com.sxz.sbs.module.client.bean.AllHousesBean;
import com.sxz.sbs.module.sale.BuildingSearchActivity;
import com.sxz.sbs.module.staff.mode.StaffIntentBean;
import com.sxz.sbs.module.staff.presenter.AllHousesPresenter;
import com.sxz.sbs.module.staff.view.AllHousesView;
import com.sxz.sbs.widget.CommonTitle;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 所有案场
 */
public class AllParkingActivity extends BaseActivity<AllHousesPresenter> implements AllHousesView {

    @BindView(R.id.tv_nav_title)
    TextView tvNavTitle;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    private AllHousesAdapter adapter;
    private List<AllHousesBean.HousesBean> dataList = new ArrayList<>();
    private int currentPage = 1;
    private String keyWords = "";
    private StaffIntentBean staffBean;
    private int fromTag;
    public int  tagIntent = 0;
    @Override
    protected void init() {
        EventBus.getDefault().register(this);
        fromTag = getIntent().getIntExtra("from_tag",0);
        if (fromTag == 0) {
            tagIntent = 2;
            staffBean = getIntent().getParcelableExtra("staff_bean");
            new CommonTitle.Builder(this).setTitle("所有楼盘").showLeftReadyImg(R.drawable.ic_search_left).build().show();
        } else {
            tagIntent = fromTag;
            new CommonTitle.Builder(this).setTitle("所有楼盘").build().show();
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getHousesData();
            }
        });
        refreshLayout.setRefreshing(true);
        getHousesData();
        adapter = new AllHousesAdapter(dataList);
        recyclerView.setAdapter(adapter);
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (fromTag == 0) {
                    staffBean.setSwapParkingId(dataList.get(position).getId());
                    staffBean.setParkingName(dataList.get(position).getName());
                    startActivity(new Intent(AllParkingActivity.this,ChoseStaffActivity.class)
                            .putExtra("staff_bean",staffBean)
                            .putExtra("chose_tag",ChoseStaffActivity.CHOSE_STAFF_SWAP_OUT)
                    );
                } else if (fromTag == 5){
                  Intent intent = new Intent();
                  intent.putExtra("parking_id",dataList.get(position).getId());
                  intent.putExtra("parking_name",dataList.get(position).getName());
                  setResult(1006,intent);
                  finish();
                }
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getHousesData();
            }
        },recyclerView);
    }
    /**
     * 获取楼盘列表
     */
    private void getHousesData () {
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("keyWords", keyWords);
        map.put("pageNumber", currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getAllHouses(map,true);
    }

    @OnClick(R.id.iv_title_left_img)
    public void onViewClicked() {
        startActivity(new Intent(this,BuildingSearchActivity.class)
                .putExtra(TypeConstant.SEARCH_BUILDING_INTENT,tagIntent).putExtra("staff_bean",staffBean));
    }

    @Override
    public void onGetHousesDataSuccess(AllHousesBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            if (bean.getList() != null && bean.getList().size() >0 ) {
                tvNavTitle.setText("所有楼盘("+bean.getTotalRow()+")");
                stateLayout.showContent();
                if (currentPage == 1) {
                    dataList.clear();
                    dataList.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                } else {
                    dataList.addAll(bean.getList());
                    adapter.notifyDataSetChanged();
                }
                adapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    dataList.clear();
                    adapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    if (dataList.size() > AppConstant.PAGE_SIZE) {
                        adapter.loadMoreEnd();
                    } else {
                        adapter.loadMoreEnd(true);
                    }
                }
            }

        }
    }

    @Override
    public void showError(int errorTag, String msg) {
        refreshLayout.setRefreshing(false);
        if (currentPage == 1) {
            if (errorTag == AppConstant.NET_ERROR_CODE) {
                stateLayout.showNetwork();
            } else {
                stateLayout.showEmpty();
            }
        } else {
            adapter.loadMoreFail();
        }
    }

    @Override
    public void loginAgain() {
        super.loginAgain();
        refreshLayout.setRefreshing(false);
        adapter.loadMoreComplete();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_all_parking;
    }

    @Override
    protected AllHousesPresenter createPresenter() {
        return new AllHousesPresenter();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onOperateSuccessEvent(OperateSuccessEvent event) {
        finish();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
